import utime
import random
from neopixel import NeoPixel
from machine import Pin
from machine import SoftI2C
from 驱动.ws2812Screen import WS2812Screen
from 驱动.debounce import *
from 驱动.mpu6050 import accel
from setting import *

sda = Pin(sda_Pin)
scl = Pin(scl_Pin)
i2c = SoftI2C(sda=sda, scl=scl, freq=40000)

# 第二个参数为加速度最大量程
# 0 ±2G   1  ±4G
# 2 ±8G   3  ±16G
mpu = accel(i2c, 0)

# 定义方向和角度的阈值
THRESHOLD_ANGLE = 0.4

SCREEN_WIDTH = 32  # 屏幕宽
SCREEN_HEIGHT = 8  # 屏幕高

num_leds = SCREEN_WIDTH * SCREEN_HEIGHT  # 像素数量(led灯珠数量)
pixels = NeoPixel(Pin(LED_PIN), num_leds + 64)  # 创建像素序列对象
screen = WS2812Screen(pixels, SCREEN_WIDTH, SCREEN_HEIGHT)  # 创建屏幕对象


# 定义贪吃蛇和食物的颜色
SNAKE_COLOR = (0, 200, 0)  # 红色
 
# 初始化贪吃蛇的位置和初始长度
snake = [(16, 4)]
snake_length = 1

x=16
y=4
k=1.3
# 定义贪吃蛇移动的方向
direction = "RIGHT"

# 游戏循环
while True:
    # 读取传感器数据
    accel_data = mpu.get_values()
    accel_x = round(accel_data['AcX'] / 16384.0, 2)
    accel_y = round(accel_data['AcY'] / 16384.0, 2) * -1
    
    x= x + (accel_x * k)
    y= y + (accel_y * k)
    
    if (x > SCREEN_WIDTH ):
        x = 1
    if (x < 0 ):
        x = SCREEN_WIDTH
    if (y > SCREEN_HEIGHT ):
        y = 1
    if (y < 0 ):
        y = SCREEN_HEIGHT
    # 清空屏幕
    screen.fill_screen()
    for segment in snake:
        print('x',x)
        print('y',y)
        screen.draw_pixel(int(x), int(y), SNAKE_COLOR)
  
    # 刷新屏幕
    screen.show()

    # 等待一段时间
    # 可以根据需要调整游戏速度
    utime.sleep(0.01)
  
        
